package com.atnjupt.web;
/**
 * @author Wizarder
 * @create 2021-05-28 15:28
 */

import com.atnjupt.bean.User;
import com.atnjupt.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    UserServiceImpl usi = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //验证请求参数
        User loginUser = usi.login(new User(1, username, password, null));
        if(loginUser == null){
            //1失败跳转当前页面
            //用户或者密码错误，信息回显示客户端
            request.setAttribute("msg","用户或者密码错误");
            //保存客户端输入的内容
            request.setAttribute("username",username);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }else{
            //2成功跳转登陆成功页面
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }
    }
}
